import { PRE_REFRESH_TOKEN_SEC } from '@/config/constants'
import moment from 'moment'
import urlQuery from 'url-query'
import { round } from 'lodash'

export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['欢迎登录', '准备吃什么呢?', '欢迎回来', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}



export function getRedirectURI () {
  if (location.search) {
    const obj = urlQuery(location.search)
    if (obj['redirect']) {
      return obj['redirect']
    } else {
      return null
    }
  } else {
    return null
  }
}

/**
 * 用于构造v-decorator指令的参数
 * @param name
 * @param desc
 * @param required
 * @param initValue
 * @returns {*[]}
 */
export function field (name, desc, required = true, initValue) {
  const options = {}
  if (required) {
    options.rules = [{ required: required, message: `${desc}不能为空` }]
  }
  if (initValue !== undefined) {
    options.initialValue = initValue
  }
  return [name, options]
}

/**
 * 抽屉组件的样式
 */
export const drawerBody = {
  height: 'calc(100% - 108px)',
  overflow: 'auto',
  paddingBottom: '108px'
}

/**
 * 抽屉组件的样式
 */
export const drawerBottom = {
  position: 'absolute',
  left: 0,
  bottom: 0,
  width: '100%',
  borderTop: '1px solid #e9e9e9',
  padding: '10px 16px',
  background: '#fff',
  textAlign: 'right'
}

export function updatePagination (pagination, result) {
  pagination.current = result.current
  pagination.pageSize = result.pageSize
  pagination.total = result.total
}

export function translateBytes (numByte) {
  if (numByte < 1024) { // 1KB以内
    return `${numByte}字节`
  } else if (numByte < 1024 * 1024) { // 1MB以内
    return `${round(numByte / 1024, 0)}KB`
  } else if (numByte < 1024 * 1024 * 1024) { // 1GB以内
    return `${round(numByte / (1024 * 1024), 2)}MB`
  } else { // 1G以上
    return `${round(numByte / (1024 * 1024 * 1024), 2)}GB`
  }
}
